package com.zhiyou.aifeng.smart_machine

import android.annotation.SuppressLint
import android.os.Bundle
import com.zhiyou.aifeng.common.base.oneClick
import com.zhiyou.aifeng.common.helper.*
import com.zhiyou.aifeng.common_ui.base.BaseActivity
import com.zhiyou.aifeng.common_ui.model.BaseUrl
import com.zhiyou.aifeng.viewmodel.WalletViewModel
import kotlinx.android.synthetic.main.activity_wallet.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.json.JSONObject

class WalletActivity : BaseActivity() {

    private var mBalance = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_wallet)
        setTransparentAndToolbar(isDarkFont = false)
        initTitle()
    }

    override fun getViewModel() = get<WalletViewModel>()

    override fun onStart() {
        super.onStart()
        getData()
    }

    override fun initTitle(title: String, name: String, divider: Boolean) {
        initListener()
    }

    override fun initListener() {
        wallet_detail.oneClick { startActivity<WalletListActivity>() }
        wallet_card.oneClick { startActivity<WalletCardActivity>() }
        bt_cash.oneClick {
            if (getString("auth") != "2") {
                toast("您还未通过实名认证，暂无法操作！")
                return@oneClick
            }

            startActivity<WalletCashActivity>()
        }
    }

    @SuppressLint("SetTextI18n")
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel().request(
            isLoading = false,
            requestUrl = BaseUrl.wallet,
            params = *arrayOf("userId" to getString("userId")),
            onResult = {
                val obj = JSONObject(it)

                mBalance = obj.optNotNull("userBalance").formatDecimal()
                putShared("balance", mBalance)

                wallet_balance.text = mBalance
                wallet_total.text = "总收入${obj.optNotNull("beforeAccount").formatDecimal()}元"
                wallet_cash.text = "已提现${obj.optNotNull("disBalance").formatDecimal()}元"
            }
        )
    }

}